$(function () {
    var StartTime = frxs.getUrlParam("StartTime");
    var EndTime = frxs.getUrlParam("EndTime");
    var SettleID = frxs.getUrlParam("SettleID");

    $("#StartTime").val(StartTime);
    $("#EndTime").val(EndTime);
    $("#SettleID").val(SettleID);


    getWarehouse();


    //搜索快捷键
    $("#StartTime,#EndTime,#SettleID").keypress(function (e) {
        if (e.keyCode == 13) {
            searchForm();
        }
    });

});





function getWarehouse() {
    $.ajax({
        url: '../User/WarehouseGet',
        type: 'post',
        data:{},
        dataType: 'json',
        success:function (obj) {
            $("#cWTel").text("财务室电话："+obj.CWTel);
        }
    });
}


//打开弹出在订单详情框
function OpenDetails(id) {
    $.ajax({
        url: "../User/GetSaleSettleModel",
        type: "post",
        data: { settleId: id.toString() },
        dataType: "json",
        success: function (obj) {
            if (obj.Flag == 0) {
                obj = obj.Data;
                $("#settleNO").text(obj.SaleSettle.SettleID);
                $("#SettleTime").text(frxs.dateTimeFormat(obj.SaleSettle.SettleTime, "yyyy/MM/dd  HH:mm"));

                var htmCode = "";
                var hj1 = 0;
                var hj2 = 0;
                //从表循环
                $(obj.SaleSettleDetailList).each(function (i) {
                    htmCode += "<tr>";
                    htmCode += "<td>" + (this.BillType == 0 ? "门店订单" : (this.BillType == 1 ? "门店退货单" : "门店费用单")) + "</td>";
                    htmCode += "<td>" + this.BillID + "</td>";
                    htmCode += "<td>" + frxs.dateTimeFormat(this.BillDate,"yyyy/MM/dd HH:mm") + "</td>";
                    htmCode += "<td>" + (this.FeeName ? this.FeeName : "-") + "</td>";
                    htmCode += "<td>" + parseFloat(this.BillPayAmt).toFixed(2) + "</td>";
                    htmCode += "<td>" + (this.Remark ? this.Remark : "-") + "</td>";
                    if (i == 0) {
                        htmCode += "<td rowspan=\"" + (obj.SaleSettleDetailList.length + 1) + "\" style=\"border-left:1px solid #ccc; text-align:center\">" + parseFloat(obj.SaleSettle.SettleAmt).toFixed(0) + "</td>";
                    }
                    htmCode += "</tr>";

                    hj1 += this.BillAmt;
                    hj2 += this.BillPayAmt;
                });



                var tempTr = $("#tabInfoTemp").html();

                $("#tabInfo").html(tempTr+htmCode);


                $(".Infobox-dialog").show();
            } else {
                alert(obj.Info);
            }
        }
    });

}



//搜索
function searchForm() {
    var StartTime = $("#StartTime").val();
    var EndTime = $("#EndTime").val();
    var SettleID = $("#SettleID").val();

    location.href = '../User/Account?StartTime=' + StartTime + "&EndTime=" + EndTime + "&SettleID=" + SettleID;
}

//清空
function resetForm() {
    $("#StartTime").val('');
    $("#EndTime").val('');
    $("#SettleID").val('');
}

//关闭支付弹层
function ClosePay() {
    $(".pay_bg").hide();
    $(".pay_mainBox").hide();
}
//打开支付弹层
function OpenPayAccount(id) {
    //加载层
    layer.load(0, { shade: [0.3, '#000'] });
    $.ajax({
        url: "../User/GetSaleSettleModel",
        type: "post",
        data: { settleId: id.toString() },
        dataType: "json",
        success: function (obj) {
            if (obj.Flag == 0) {
                bindCode(obj);

                //关闭load
                layer.closeAll();

                //打开支付层
                $(".pay_bg").show();
                $(".pay_mainBox").show();
                //数据小于5调的样式调整
                if ($(".ordingContent li").length < 5) {

                    $(".pay_mainBox").css("height", "auto");
                    var h= $(".ordingContent ul").height()+20;
                    $(".ordingContent").css("height", h);
                    // $(".ordingContent").css("overflow-y", hidden);
                    $(".pay_mainBox").css("paddingBottom", "20px");
                    $(".pay_mainBox").css("marginTop", -($(".pay_mainBox").height()/2));
                };
            }
        }
    });
}


//绑定代码
function bindCode(obj) {
    if (obj.FlagDescription == "SUCCESS" && obj.Data != null) {
        //支付金额
        $("#settleAmt").text(parseFloat(obj.Data.SaleSettle.SettleAmt).toFixed(0));

        //显示冻结还是未冻结层
        if (obj.Data.RedirectType == 2) {
            $("#payOutTimeDiv1").hide();
            $("#payOutTimeDiv2").show();
        }

        //多少小时
        $(".payOutTime").text(obj.Data.PayOutTime);

        var saleSettleList = obj.Data.SaleSettleDetailList;

        var saleSettleListCode = "";
        //单据列表
        $(saleSettleList).each(function () {
            saleSettleListCode += "<li>" + this.BillTypeStr + "：" + this.BillID + "<span><i>￥</i>" + this.BillPayAmt + "</li>";
        });
        $("#saleSettleList").html("");
        $("#saleSettleList").append(saleSettleListCode);

        //绑定去支付URL
        var settleId = obj.Data.SaleSettle.SettleID;
        $("#goPayLink").attr("href", "../LinePay/PayQR?settlementId=" + settleId);
        $("#goPayLinkRes").attr("href", "../LinePay/PayQR?settlementId=" + settleId);

        //支付检测
        $("#btnSuc,#btnRes").click(function () {
            isPayCheck(settleId,true);
        });
        $("#spanSettleId").text(settleId);

        //弹出显示
        //打开支付层
        $(".pay_bg").show();
        $(".pay_mainBox").show();
    }
}/**
 * Created by jiali on 2017/2/23.
 */
